import i18n from 'i18next';
import { transformPart } from '../common/part-and-transform/funcs';
import { PartAndTransformDialog } from '../common/part-and-transform/PartAndTransformDialog';
import { PartAndTransformCmdParams } from '../common/part-and-transform/types';

async function finishTransform({
  formData,
  matrix,
  onClose,
  docTree,
}: PartAndTransformCmdParams) {
  const part = docTree.children.find(
    ({ key }) => key === formData.select?.idInfo?.o,
  );

  await transformPart(part, formData, matrix);

  onClose();
}

export const cmdTransformPart = {
  id: 'CmdTransformPart',
  label: 'command.transform-part-or-product',
  icon: 'commands/product/transform',
  create: (label?: string, onClose?: () => void) => (
    <PartAndTransformDialog
      key={cmdTransformPart.id}
      title={i18n.t(label)}
      onClose={onClose}
      executeCmd={finishTransform}
      hideOriginal={true}
    />
  ),
};
